package cn.readjava.controller.manage;

import cn.readjava.bean.VideoCategory;
import cn.readjava.service.VideoCategoryService;
import cn.readjava.util.ResponseResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/manage/video/category")
public class ManageVcController {

    @Resource
    private VideoCategoryService videoCategoryService;

    // 删除
    @GetMapping("/delete/{id}")
    @ResponseBody
    public Object delete(@PathVariable Long id) {
        try {
            videoCategoryService.deleteById(id);
            return new ResponseResult(200, "删除成功", null);
        } catch (Exception e) {
            return new ResponseResult(100, "删除失败", null);
        }
    }

    // 预新增
    @GetMapping("/add")
    public String add() {
        return "manage/manage-vc-add";
    }

    // 新增
    @PostMapping("/add")
    @ResponseBody
    public Object add(VideoCategory vc) {
        VideoCategory videoCategory = videoCategoryService.save(vc);
        if (videoCategory != null) {
            return new ResponseResult(200, "新增成功", videoCategory);
        }
        return new ResponseResult(100, "新增失败", null);
    }

    // 预更新
    @GetMapping("/get/{id}")
    public String get(@PathVariable Long id, Model model) {
        VideoCategory videoCategory = videoCategoryService.getOne(id);
        model.addAttribute("videoCategory", videoCategory);
        return "manage/manage-vc-update";
    }

    // 更新
    @PostMapping("/update")
    @ResponseBody
    public Object update(VideoCategory vc) {
        VideoCategory videoCategory = videoCategoryService.save(vc);
        if (videoCategory != null) {
            return new ResponseResult(200, "更新成功", videoCategory);
        }
        return new ResponseResult(100, "更新失败", null);
    }

    // 列表
    @GetMapping("/list")
    public String list(Model model) {
        List<VideoCategory> VCList = videoCategoryService.findAll();
        model.addAttribute("VCList", VCList);
        return "manage/manage-vc";
    }
}
